package com.jxdinfo.hussar.cloud.module.example.tjjc.towerdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towerdata.vo.TowerDataSearchVO;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towerdata.vo.TowerDataVO;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.platform.core.utils.HussarUtils;
import com.jxdinfo.hussar.support.exception.HussarException;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towerdata.model.TowerData;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towerdata.service.TowerDataService;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towerdata.dao.TowerDataMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 塔机实时数据服务实现类
 */
@Service("tjjc.towerdata.TowerDataServiceImpl")
@HussarDs("master")
public class TowerDataServiceImpl extends HussarServiceImpl<TowerDataMapper, TowerData> implements TowerDataService {

    private static final Logger logger = LoggerFactory.getLogger(TowerDataServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private TowerDataMapper towerDataMapper;

    @Override
    public ApiResponse<List<TowerDataVO>> findRealTimeList(TowerDataSearchVO towerDataSearchVO) {
        QueryWrapper<TowerData> wrapper = new QueryWrapper<>();
        wrapper.eq("ty_project_code", towerDataSearchVO.getTyProjectCode());
        wrapper.eq("tower_id", towerDataSearchVO.getTowerId());
        List<TowerData> towerDataList = towerDataMapper.selectList(wrapper);
        List<TowerDataVO> resultList = towerDataList.stream().map(towerData -> {
            TowerDataVO towerDataDto = new TowerDataVO();
            BeanUtils.copyProperties(towerData, towerDataDto);
            return towerDataDto;
        }).collect(Collectors.toList());
        return ApiResponse.success(resultList);
    }

    @Override
    public ApiResponse<Map<String, Long>> findTowerRealTimeAlarmCount(TowerDataSearchVO towerDataSearchVO) {
        Map<String, Long> alarmMap = new HashMap<>();

        //获取报警数量
        QueryWrapper<TowerData> wrapper = new QueryWrapper<>();
        wrapper.eq("ty_project_code", towerDataSearchVO.getTyProjectCode());
        wrapper.eq("tower_id", towerDataSearchVO.getTowerId());
        wrapper.eq("data_status", 2);
        long alarmCount = towerDataMapper.selectCount(wrapper);

        alarmMap.put("alarmCount",alarmCount);
        return ApiResponse.success(alarmMap);
    }
}
